package com.example.dryyzx.controller;

import com.example.dryyzx.dao.FoodMapper;
import com.example.dryyzx.dao.OrderMapper;
import com.example.dryyzx.encapsulation.*;
import com.example.dryyzx.entity.Food;
import com.example.dryyzx.entity.JsonResult;
import com.example.dryyzx.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class FoodController {

    @Autowired
    private FoodMapper foodMapper;
    @Autowired
    private OrderMapper orderMapper;

    private List<FoodCalendarPageEncapsulation> change(List<Food> food,int limit){
        List<FoodCalendarPageEncapsulation> encapsulation=new ArrayList<FoodCalendarPageEncapsulation>();
        for(int i=0;i<limit;i++){
            FoodCalendarPageEncapsulation tmp=new FoodCalendarPageEncapsulation();
            tmp.id=food.get(i).getId();
            tmp.price=food.get(i).getPrice();
            tmp.name=food.get(i).getName();
            tmp.type=food.get(i).getType();
            tmp.tag=food.get(i).getTag();
            if(food.get(i).getHalal()==true)
                tmp.is_muslim="是";
            else
                tmp.is_muslim="否";
            encapsulation.add(i,tmp);
        }
        return encapsulation;
    }

    @RequestMapping(value = "food_calendar_page/get_calendar",method = RequestMethod.POST)
    public @ResponseBody
    String showFoodByDay(String day){
        if(day.equals("周一")||day.equals("周二")||day.equals("周三")||day.equals("周四")||day.equals("周五")||day.equals("周六")||day.equals("周日"));
        else
            return new JsonResult(false, "请输入周一到周日的字段").toString();

        List<Food> breakfast=foodMapper.selectByDayTime(day,"早餐");
        List<Food> lunch=foodMapper.selectByDayTime(day,"午餐");
        List<Food> dinner=foodMapper.selectByDayTime(day,"晚餐");

        FoodCalendarPageEncapsulation2 total_data=new FoodCalendarPageEncapsulation2();
        total_data.breakfast_list=change(breakfast,breakfast.size());
        total_data.lunch_list=change(lunch,lunch.size());
        total_data.dinner_list=change(dinner,dinner.size());

        return new JsonResult(true, total_data).toString();
    }

    private FoodDistributePage2Encapsulation change3(Food food,int cid){
        FoodDistributePage2Encapsulation tmp=new FoodDistributePage2Encapsulation();
        tmp.id=food.getId();
        tmp.name=food.getName();
        tmp.tag=food.getTag();
        Order currentOrder = orderMapper.selectByCidFid(cid,food.getId());
        if(currentOrder==null)
            tmp.chosen=false;
        else
            tmp.chosen=true;
        return tmp;
    }

    private FoodDistributePage2Encapsulation3 change2(List<Food> food,int limit,int cid){
        FoodDistributePage2Encapsulation3 total_data = new FoodDistributePage2Encapsulation3();

        FoodDistributePage2Encapsulation2 stapleFood = new FoodDistributePage2Encapsulation2("主食");
        FoodDistributePage2Encapsulation2 bigMeat = new FoodDistributePage2Encapsulation2("大荤");
        FoodDistributePage2Encapsulation2 smallMeat = new FoodDistributePage2Encapsulation2("小荤");
        FoodDistributePage2Encapsulation2 vegetable = new FoodDistributePage2Encapsulation2("素菜");
        FoodDistributePage2Encapsulation2 soup = new FoodDistributePage2Encapsulation2("汤");
        FoodDistributePage2Encapsulation2 setMeal = new FoodDistributePage2Encapsulation2("套餐");
        FoodDistributePage2Encapsulation2 snack = new FoodDistributePage2Encapsulation2("点心");
        FoodDistributePage2Encapsulation2 fruit = new FoodDistributePage2Encapsulation2("水果");

        for(int i=0;i<limit;i++){
            if(food.get(i).getType().equals("主食"))
                stapleFood.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("大荤"))
                bigMeat.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("小荤"))
                smallMeat.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("素菜"))
                vegetable.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("汤"))
                soup.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("套餐"))
                setMeal.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("点心"))
                snack.list.add(change3(food.get(i),cid));
            if(food.get(i).getType().equals("水果"))
                fruit.list.add(change3(food.get(i),cid));
        }

        if(stapleFood.list.size()>0)
            total_data.food_calendar.add(stapleFood);
        if(bigMeat.list.size()>0)
            total_data.food_calendar.add(bigMeat);
        if(smallMeat.list.size()>0)
            total_data.food_calendar.add(smallMeat);
        if(vegetable.list.size()>0)
            total_data.food_calendar.add(vegetable);
        if(soup.list.size()>0)
            total_data.food_calendar.add(soup);
        if(setMeal.list.size()>0)
            total_data.food_calendar.add(setMeal);
        if(snack.list.size()>0)
            total_data.food_calendar.add(snack);
        if(fruit.list.size()>0)
            total_data.food_calendar.add(fruit);

        return total_data;
    }

    @RequestMapping(value = "food_distribute_page/search_calendar",method = RequestMethod.POST)
    public @ResponseBody
    String showFoodByTime(String day,String time,int id){
        if(day.equals("周一")||day.equals("周二")||day.equals("周三")||day.equals("周四")||day.equals("周五")||day.equals("周六")||day.equals("周日"));
        else
            return new JsonResult(false, "请输入周一到周日的字段").toString();
        if(time.equals("早餐")||time.equals("午餐")||time.equals("晚餐"));
        else
            return new JsonResult(false, "请输入早餐、午餐或晚餐").toString();

        List<Food> foodList=foodMapper.selectByDayTime(day,time);

        FoodDistributePage2Encapsulation3 total_data=change2(foodList,foodList.size(),id);

        if(total_data.food_calendar==null)
            return new JsonResult(false, "没有数据或无法查找到数据").toString();
        return new JsonResult(true, total_data).toString();
    }

    @RequestMapping(value = "food_calendar_page/add_food_info",method = RequestMethod.POST)
    public @ResponseBody
    String addFood(String name, int price, String type, String tag, String day, String time,String is_muslim){
        Food food = new Food();
        food.setName(name);
        food.setPrice(price);
        food.setType(type);
        food.setTag(tag);
        if(day=="")
            food.setDay(null);
        else
            food.setDay(day);
        if(time=="")
            food.setTime(null);
        else
            food.setTime(time);
        if(is_muslim.equals("是"))
            food.setHalal(true);
        else if(is_muslim.equals("否"))
            food.setHalal(false);
        else
            return new JsonResult(false,"是否清真数据请选择是或否").toString();

        int insert = foodMapper.insert(food);
        if(insert == 0 )
            return new JsonResult(false, "无法添加食物").toString();
        return new JsonResult(true, insert).toString();
    }

    @RequestMapping(value = "food_calendar_page/delete_food",method = RequestMethod.POST)
    public @ResponseBody
    String deleteFood(int id){
        Food currentFood = foodMapper.selectByPrimaryKey(id);
        if(currentFood ==null)
            return new JsonResult(false, "找不到要删除的食物").toString();

        int delete = foodMapper.deleteByPrimaryKey(id);
        if(delete == 0)
            return new JsonResult(false, "无法删除食物").toString();
        return new JsonResult(true, delete).toString();
    }
}
